from utils.maze_gui import MazeGui
from PyQt5.QtCore import Qt
from copy import deepcopy


class DfsQMaze(MazeGui):
    def __init__(self, a=5, b=20):
        super(DfsQMaze, self).__init__(a, b) # 父类初始化
        self.path = list()
        self.poped = list()
        self.path.append(deepcopy(self.ENTRANCE))
        self.dfs(deepcopy(self.ENTRANCE))

    def paintEvent(self, paintEvent):
        """
        【注】QWidigets类的方法，所有画图的操作放在这里。实例化时，会自动调用这个方法。
        """
        super(DfsQMaze, self).paintEvent(paintEvent) #父类绘画界面初始化


        self.paintDfs()

    def paintDfs(self):
        """将递归的过程画出来，必须通过`paintEvent()`方法调用
        【注】如果在`dfs()`中使用了相关画图、画线等函数，即可在窗口中绘制出来
        """
        self.draw_point(self.ENTRANCE, self.pen, self.painter, _color=Qt.blue)
        self.draw_point(self.EXIT, self.pen, self.painter, _color=Qt.red)
        # self.draw_point([0,0],self.pen, self.painter,_color=Qt.yellow)
        del self.pen, self.painter

    def dfs(self,current, isGo = True):
        """在此写下代码
        """
        def check(current):
            yy = len(self.MAZE)
            xx = len(self.MAZE[0])
            if 0 <= current[0] < yy and 0 <= current[1] <xx and current not in self.path:
                return self.MAZE[current[0]][current[1]] == 0
            else:
                return False

        if current[0] == self.EXIT[0] and current[1] == self.EXIT[1]:
            isGo = False
            print(111,self.path)
            print(222,self.poped)
            return
        else:
            for (d_x, d_y) in [(1, 0), (0, -1), (0, 1), (-1, 0)]:
                if not isGo:
                    return
                current[1] += d_x
                current[0] += d_y
                if check(current):
                    self.path.append(deepcopy(current))
                    self.dfs(current)
                    self.poped.append(self.path.pop())
                    current = deepcopy(self.path[-1])
                else:
                    current[1] -= d_x
                    current[0] -= d_y




if __name__ == "__main__":
    from PyQt5.QtWidgets import QApplication
    import sys

    application = QApplication(sys.argv)
    solver = DfsQMaze()
    sys.exit(application.exec_())
